//package src;
import java.util.*;
import java.awt.*;
import java.awt.geom.*;

import javax.swing.*;



import java.awt.event.*;
import java.awt.Component.*;

public class GraphicsObject extends JComponent
{	
	//************************
	//~~~~~DECLARATIONS~~~~~ *
	//************************
	
	private Graphics2D graphics;
	
	// RGB	
	private int r;		// Red Component of Color
	private int g;		// Blue Component of Color
	private int b;		// Green Component of Color
	
	// Line Width
	private int lineWidth;		// Width of the line drawn
	
	// Number of Points
	private int numPoints = 0;		// Number of points on the object
	
	// ArrayList for points
	private ArrayList<Point2D.Double> objectPoints;		// X-Y Coordinates for each object
	
	//*********************
	// ~~~~~METHODS~~~~~  *
	//*********************
	
	// Constructor
	public GraphicsObject()
	{
		
		setR(255);		// Set the default color to BLACK
		setG(255);
		setB(255);
		setLineWidth(1);		// Set the default line width to 1 pixel
		objectPoints = new ArrayList<Point2D.Double>(0);
	}
	// Set the red component of the color
	public void setR(int red)
	{
		r = red;
	}
	// Set the green component of the color
	public void setG(int green)
	{
		g = green;
	}
	// Set the blue component of the color
	public void setB(int blue)
	{
		b = blue;
	}
	// Setting the red, green, and blue components of the color
	public void setRGB(int red, int green, int blue)
	{
		r = red;
		g = green;
		b = blue;
	}
	// Set the line width of the object
	public void setLineWidth(int lw)
	{
		lineWidth = lw;
	}
	// Adding a new point to the ArrayList of Point2D points
	public void addPoint(Point2D.Double p2d)
	{
		objectPoints.add(p2d);
		numPoints++;
	}
	public void displayPoint(int i)
	{
		System.out.println(objectPoints.get(i));
	}
	// Retrieving the red component of the color
	public int getR()
	{
		return r;
	}
	// Retrieving the green component of the color
	public int getG()
	{
		return g;
	}
	// Retrieving the blue component of the color
	public int getB()
	{
		return b;
	}
	// Retrieving the red, green, and blue components of the color in array format
	public int[] getRGB()
	{
		int[] rgb = {r, g, b};
		return rgb;
	}
	// Retrieving the line width of the object
	public int getLineWidth()
	{
		return lineWidth;
	}
	// Retrieving the number of objects
	public int getNumPoints()
	{
		return numPoints;
	}
	
	// Draw
	public void draw(PadDraw pd)
	{
		//setDoubleBuffered(true);
		double currentX;
		double currentY;
		double oldX;
		double oldY;
		Point2D.Double p2d;
		System.out.println(getNumPoints());
		for(int g = 1; g < getNumPoints(); g++)
		{
			displayPoint(g);
			p2d = objectPoints.get(g);
			currentX = p2d.getX();
			currentY = p2d.getY();
			p2d = objectPoints.get(g-1);
			oldX = p2d.getX();
			oldY = p2d.getY();
			if(graphics != null)
            graphics.drawLine((int)oldX, (int)oldY, (int)currentX, (int)currentY);
			//System.out.println(oldX + " " + oldY + "," + currentX + " " + currentY);
            
            
		}
		pd.paintComponent(graphics);
		repaint();
	}
	
	// To String
	public String objectToString()
	{
		String s = " ~~~~" + getR() + "~" + getG() + "~" + getB() + "~~~" + getLineWidth() + "~~~" + getNumPoints() + "~~~";
		for(int i = 0; i < this.getNumPoints(); i++)
		{
			s = s + objectPoints.get(i).getX() + "~" + objectPoints.get(i).getY() + "~~";
		}
		s = s.substring(0, s.length()-2);
	
		return s;
	}
	
	// From String
		public void stringToObject(String s, PadDraw pd)
		{
			
			s = s.substring(56);
			System.out.println(s);
			double x;
			double y;
			int max = 0;
			String [] obj = s.split("~~~~");
			String [][] objSec = new String [obj.length][3];
			
			//ArrayList<ArrayList<String>> pairs = new ArrayList<ArrayList<String>>(0);
			
			for(int a = 0; a < obj.length; a ++)
			{
				objSec [a] = obj[a].split("~~~");
				//pairs.set(a, (ArrayList<String>) Arrays.asList(objSec[a][3].split("~~")));
			}
			String [][] colorArray = new String [obj.length][3];
			String [] widthArray = new String [obj.length];
			String [] numPointsArray = new String [obj.length];
			//ArrayList<ArrayList<ArrayList<String>>> pointsArrayList = new ArrayList<ArrayList<ArrayList<String>>>(0);
			
			for(int b = 0; b < obj.length; b ++)
			{
				System.out.println("hello");
				colorArray[b] = objSec[b][0].split("~");
				widthArray[b] = objSec[b][1];
				numPointsArray[b] = objSec[b][2];
				if((int)Double.parseDouble(numPointsArray[b]) > max)
					max = (int)Double.parseDouble(numPointsArray[b]);
				//String[] array = new String [pairs]
				//for(int c = 0; c < pairs.size(); c++)
				//{
				//	pointsArrayList.set(c, (ArrayList<String>) pairs.) = pairs[b][c].split("~");
				//}
			}
			

			String [] pairs = new String [max];
			pairs = objSec[0][3].split("~~");
			
			
			
			String [][] pointsArray = new String [max][2];
			
			for(int d = 0; d < max; d++)
			{
				pointsArray[d] = pairs[d].split("~");
			}
			
			
			
			System.out.println(pointsArray.length + "YEAH");
			System.out.println(pointsArray[0][0]);
			System.out.println(pointsArray[1][0]);
			
			for(int i = 0; i < pointsArray.length; i++)
			{
				//for(int j = 0; j < pointsArray[i].length; j++)
				//{
				
					Point2D.Double p2d = new Point2D.Double();
					x = Double.parseDouble(pointsArray[i][0]);
					y = Double.parseDouble(pointsArray[i][1]);
					p2d.setLocation(x, y);
				
					addPoint(p2d);
				//}
			}
			
			draw(pd);
			
		}

	
}